-- 13.lua
-- Created by zouyb Jun/26/2015
-- 蜥蜴BUFF光效

return {
    onAppear = function(monster)    
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 计算当前蜥蜴的个数    
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        local curMonsterGrid;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                curMonsterGrid = monsterGrid;
                count = count + 1;
            end
        end

        print("13 appr count:"..count);
        if count == 1 then
            -- 蜥蜴首次出现，该蜥蜴播放“怪物出现”光效
            playMonsterFirstAppearEffect(curMonsterGrid);

            -- 其他非蜥蜴怪物播放“属性增益”光效
            for i = 1, #openMonsterGrids do
                local monsterGrid = openMonsterGrids[i];
                if monsterGrid.gridData.monster ~= monster then
                    playAddBuffEffect(monsterGrid, BUFF_TYPE_HP);
                end
            end

            local function onOtherMonsterAppear(params)
                local pos = params.pos;
                local grid = DungeonM.getGridByPos(pos);
                if grid.monster and UIDungeonMgr:getCurLevel() ~= nil then
                    local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
                    for i = 1, #openMonsterGrids do
                        local monsterGrid = openMonsterGrids[i];
                        -- 其他怪物出现时回调，如果发现场上有蜥蜴，非蜥蜴怪物播放"属性增益"光效    
                        if monsterGrid.gridData.monster == grid.monster and
                           monsterGrid.gridData.monster:query("base_class_id") ~= baseClassId then
                            playAddBuffEffect(monsterGrid, BUFF_TYPE_HP);
                        end
                    end
                end            
            end    

            -- 注册其他怪物出现时回调，如果发现场上有蜥蜴，非蜥蜴怪物播放"属性增益"光效
            UIDungeonMgr:getCurLevel().monsterBuffEffectM:addEventModule("monster13_appear");
            EventMgr.removeAll("monster13_appear");
            EventMgr.register("monster13_appear", event.UI_GRID_OPEN, function(params)
                onOtherMonsterAppear(params);
            end);    
            EventMgr.register("monster13_appear", event.GRID_REFRESH_MONSTER, function(params)
                onOtherMonsterAppear(params);
            end);
--        else
--            -- 其他蜥蜴播属性增益光效    
--            for i = 1, #openMonsterGrids do
--                local monsterGrid = openMonsterGrids[i];
--                if monsterGrid.gridData.monster:query("base_class_id") == baseClassId and not monsterGrid.hasPlayedBuff then
--                    playAddBuffEffect(monsterGrid, BUFF_TYPE_HP);
--                    monsterGrid.hasPlayedBuff = true;
--                end
--            end
        end
    end,

    onDie = function(monster)    
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 计算当前蜥蜴的个数    
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        local curMonsterGrid;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                curMonsterGrid = monsterGrid;
                count = count + 1;
            end
        end

        print("13 left count:"..count);
        if count == 0 then
            -- 场上只剩下一只蜥蜴，在该蜥蜴死亡时，其他怪物播放“属性减益”光效    
            local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
            for i = 1, #openMonsterGrids do
                local monsterGrid = openMonsterGrids[i];
                if monsterGrid.gridData.monster ~= monster then
                    playAddDeBuffEffect(monsterGrid, DEBUFF_TYPE_HP);
                end
            end
            
            EventMgr.removeAll("monster13_appear");
--        elseif count == 1 then 
--            -- 场上两只蜥蜴，一只蜥蜴死亡时，另一只播放“属性减益”光效 
--            local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
--            for i = 1, #openMonsterGrids do
--                local monsterGrid = openMonsterGrids[i];
--                if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
--                    playAddDeBuffEffect(monsterGrid, DEBUFF_TYPE_HP);
--                    monsterGrid.hasPlayedBuff = false;
--                end
--            end                
        end
    end,
};